/**
 * 组织管理
 * @author yuan_rz
 * @date 2019/01/02 09:48
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            treeData: []
        },
        table_where = {
            org_cascade_id: '0',
            org_id: '0',
            cascade: '1'
        },
        cols = [[
              {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left',hide : true}
            , {field: 'NAME', width: 150, title: '部门名称', fixed: 'left', sort: true, search: 'default'}
            , {field: 'SORT_NO', width: 80, title: '排序号'}
            , {field: 'IS_AUTO_EXPAND', width: 80, title: '自动展开',filterDatas: app.datas_('sys_org_is_auto_expand')}
            , {field: 'IS_LEAF', width: 80, title: '叶子节点',filterDatas: app.datas_('sys_org_is_leaf')}
            , {field: 'TYPE', width: 80, title: '部门类型',filterDatas: app.datas_('sys_org_type')}
            , {field: 'BIZ_CODE', width: 80, title: '扩展码'}
            , {field: 'REMARK', width: 80, title: '备注'}
            , {
                width: 60,
                title: '',
                toolbar: '#script-orgsetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        orgNodeData,
        selectNode,
        orgSettingAction
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'org',
                _searchs: [{
                    id: 'org', tableId: 'org_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'org_edit', tpl: 'system/org_edit'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'org_table'),
                    url: g_ctxPath + 'sys/org/getOrgsList',
                    exportsUrl: g_ctxPath + 'sys/org/exportOrgs',
                    exportsName: '部门数据',
                    title: '部门信息表',
                    id: 'org_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],//, 'exports'
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-orgsetting-action').innerHTML).render({}, function (string) {
                    orgSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(org_table)', function (obj) {
                    layer.tips(orgSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'org_edit',
                                            tagData: {
                                                obj: obj,
                                                detail: false
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '部门编辑',
                                                area: ['600px', '360px']
                                            }
                                        });
                                        break;
                                    case '2':
                                        app.confirm({
                                            msg: '是否删除选择的部门？',
                                            ok: function () {
                                                self.deleteOrg(obj);
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(org_table)', function (obj) {
                    self._openTplFull({
                        tag: 'org_edit',
                        tagData: {
                            obj: obj,
                            detail: true
                        },
                        index: 0,
                        openOpts: {
                            title: '部门编辑',
                            area: ['600px', '360px']
                        }
                    });
                });

                //监听事件
                table.on('toolbar(org_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'org_edit',
                                tagData: {
                                    is_add: true,
                                    parent_id: selectNode.id,
                                    parent_name: selectNode.name,
                                    orgNodeData: orgNodeData
                                },
                                index: 0,
                                openOpts: {
                                    title: '部门编辑',
                                    area: ['600px', '360px']
                                }
                            });
                            break;
                        case 'delete':
                            layer.msg('删除');
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'org_table', where: table_where});
        },
        deleteOrg: function (obj) {
            app.ajax({
                xurl: 'sys/org/delete',
                xdata: obj.data,
                loading: true,
                okmsg: '组织信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                        self.deleteNode(obj.data.ID);
                    }
                }
            });
        },
        initTree: function () {
            app.ajax({
                xurl: 'sys/org/getOrgsTree',
                async:false,
                cb: function (result) {
                    if (result.ok) {
                        orgNodeData = result.data.data;
                        vueData.treeData.pushArray(orgNodeData);
                        selectNode = orgNodeData[0];
                        table_where.org_cascade_id = selectNode.code;
                        table_where.org_id = selectNode.id;
                    }
                }
            });
        },
        updateTree:function() {
            app.ajax({
                xurl: 'sys/org/getOrgsTree',
                async:false,
                cb: function (result) {
                    if (result.ok) {
                        orgNodeData = result.data.data;
                        vueData.treeData = orgNodeData;
                        selectNode = orgNodeData[0];
                        table_where.org_cascade_id = selectNode.code;
                        table_where.org_id = selectNode.id;
                    }
                }
            });
        },
        addNode: function(node){
            self.vues['page'].$refs.tree.append(node,node.parentid);
        },
        editNode: function(id,name){
            self.vues['page'].$refs.tree.getNode(id).data.name = name;
        },
        deleteNode: function(id){
            self.vues['page'].$refs.tree.remove(id);
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'org',
                    methods: {
                        handleNodeClick: function (node) {
                            selectNode = node;
                            table_where.org_cascade_id = node.code;
                            table_where.org_id = node.id;
                            self._reloadTable({id: 'org_table', curr: 1, where: table_where});
                        },
                        queryData: function(){
                            self._reloadTable({id: 'org_table', curr: 1, where: table_where});
                        }
                    }
                }]
            });
            // app.extend(table_where, searchObj.fields);
            self.initTree();
            self.initTable();
        },
        _lazy: function () {
            this._super();
            var treeH = document.querySelector('.el-tree');
            var tableH = document.querySelector('.layui-form').clientHeight;
            treeH.style.height = (tableH + 2) +'px';

            $(window).resize(function() {
                var tableH = document.querySelector('.layui-form').clientHeight;
                treeH.style.height = (tableH + 2) +'px';
            })
        }
    });

});
